package com.ruoyi.framework.shiro.authc;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.shiro.enums.LoginType;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.pac4j.core.profile.UserProfile;

import java.util.Map;
import java.util.Objects;

public class CustomizedToken extends UsernamePasswordToken {

    private UserProfile profile;

    private String code;

    private Map<String, Object> soapMessage;

    private LoginType loginType;

    public CustomizedToken(final String username, final String password, final boolean rememberMe, final LoginType loginType) {
        super(username, password, rememberMe);
        this.loginType = loginType;
    }

    public CustomizedToken(final UserProfile profile, final LoginType loginType) {
        this.profile = profile;
        this.loginType = loginType;
    }

    public CustomizedToken(final String code, final LoginType loginType) {
        this.code = code;
        this.loginType = loginType;
    }

    public CustomizedToken(final String username, Map<String, Object> soapMessage, final LoginType loginType) {
        super(username, (char[]) null, false);
        this.loginType = loginType;
        this.soapMessage = soapMessage;
    }

    @Override
    public Object getPrincipal() {
        if (profile != null) {
            return profile;
        }

        if (code != null) {
            return code;
        }

        return super.getPrincipal();
    }

    @Override
    public Object getCredentials() {
        if (profile != null) {
            return profile.hashCode();
        }

        if (code != null) {
            return code.hashCode();
        }

        if (Objects.nonNull(soapMessage) && StringUtils.isNotNull(getUsername())) {
            return getUsername().hashCode();
        }

        return super.getCredentials();
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public UserProfile getProfile() {
        return profile;
    }

    public void setProfile(UserProfile profile) {
        this.profile = profile;
    }

    public Map<String, Object> getSoapMessage() {
        return soapMessage;
    }

    public void setSoapMessage(Map<String, Object> soapMessage) {
        this.soapMessage = soapMessage;
    }

    public LoginType getLoginType() {
        return loginType;
    }

    public void setLoginType(LoginType loginType) {
        this.loginType = loginType;
    }


}
